package ro.dandanciu.webgallery.ui.galleries;

import java.util.List;

import org.apache.log4j.Logger;

import ro.dandanciu.webgallery.domainmodel.Gallery;
import ro.dandanciu.webgallery.domainmodel.Image;
import ro.dandanciu.webgallery.exceptions.ApplicationException;
import ro.dandanciu.webgallery.services.ImageServices;
import ro.dandanciu.webgallery.ui.general.WebGalleryAction;

public class ImagesAction extends WebGalleryAction {

	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GalleryUploadAction.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = 8222604444042363505L;

	private static final String LIST = "L";

	private static final String PUBLIC_LIST = "PL";

	private static final String LIST_FORWARD = "images";
	
	private String mode ="";
	
	private long galleryId = -1;
	
	private Gallery gallery = null;
	
	private List<Image> images = null;
	
	private String path = "";
	
	private ImageServices imageServices;
	
	public ImagesAction(ImageServices imageServices) {
		this.imageServices = imageServices;
	}
	

	
	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start");
		}

		String forward = null;
		if (LIST.equals(mode)) {
			forward = executeList();
		}else if (PUBLIC_LIST.equals(mode)) {
			forward = executePublicList();
		} else {
			forward = setMessage("failure");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("forward: " + forward);
			logger.debug("execute() - end");
		}
		return forward;
	}
	
	private String executePublicList() {
		if (galleryId > 0) {
			gallery = galleryServices.getGalleryById(galleryId);
			if (gallery.isPublicAccess()) {
				images = imageServices.getImagesForGallery(gallery);
				path = "/";
				Gallery tempGallery = gallery;
				while (tempGallery != null) {
					path = "/" + tempGallery.getName().trim() + path;
					tempGallery = tempGallery.getParent();
				}
				path = "/public/" + gallery.getOwner().getUserId() + path;
			} else {
				return setMessage("gallery.not.authorized");
			}
		} else {
			throw new ApplicationException(); //TODO add reason
		}
		return LIST_FORWARD;
	}


	private String executeList() {
		if (galleryId > 0) {
			gallery = galleryServices.getGalleryById(galleryId);
			images = imageServices.getImagesForGallery(gallery);
			path = "/";
			Gallery tempGallery = gallery;
			while (tempGallery != null) {
				path = "/" + tempGallery.getName().trim() + path;
				tempGallery = tempGallery.getParent();
			}
			path = "/" + getSessionUser().getUserId() + path;
		} else {
			throw new ApplicationException(); //TODO add reason
		}
		return LIST_FORWARD;
	}

	public String getMode() {
		return mode;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}


	public long getGalleryId() {
		return galleryId;
	}


	public void setGalleryId(long galleryId) {
		this.galleryId = galleryId;
	}


	public List<Image> getImages() {
		return images;
	}


	public void setImages(List<Image> images) {
		this.images = images;
	}


	public String getPath() {
		return path;
	}


	public void setPath(String path) {
		this.path = path;
	}


	public Gallery getGallery() {
		return gallery;
	}


	public void setGallery(Gallery gallery) {
		this.gallery = gallery;
	}

}
